package com.airS.task.fileMgr.utils.httpConvert;

import com.airS.task.fileMgr.common.response.BaseResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by alan on 2017/3/9.
 */
public class MessageConverterFastJson extends AbstractHttpMessageConverter<Object> {
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    public static final CustomSerializeFilter CUSTOM_SERIALIZE_FILTER = new CustomSerializeFilter();

    private SerializerFeature[] serializerFeature;

    public MessageConverterFastJson() {
        super(new MediaType("application", "json", DEFAULT_CHARSET));
    }

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        return true;
    }

    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        return true;
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        throw new UnsupportedOperationException();
    }

    @Override
    protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i;
        while ((i = inputMessage.getBody().read()) != -1) {
            baos.write(i);
        }
        return JSON.parseArray(baos.toString(), clazz);
    }

    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        if (o instanceof BaseResponse) {
            BaseResponse response = (BaseResponse) o;
            String callback = response.getCallback();
            if (StringUtils.hasText(callback)) {
                JSONPObject jsonpObject = new JSONPObject(callback);
                jsonpObject.addParameter(response);
                o = jsonpObject;
            }
        }
        String jsonString = JSON.toJSONString(o, getSerializeConfig(), CUSTOM_SERIALIZE_FILTER, this.serializerFeature);
        OutputStream out = outputMessage.getBody();
        out.write(jsonString.getBytes(DEFAULT_CHARSET));
        out.flush();
    }

    public void setSerializerFeature(SerializerFeature[] serializerFeature) {
        this.serializerFeature = serializerFeature;
    }

    private static SerializeConfig getSerializeConfig() {
        SerializeConfig sc = new SerializeConfig();

        sc.put(Date.class, new SimpleDateFormatSerializer(YYYY_MM_DD_HH_MM_SS));
        sc.put(java.sql.Date.class, new SimpleDateFormatSerializer(YYYY_MM_DD_HH_MM_SS));
        sc.put(Timestamp.class, new SimpleDateFormatSerializer(YYYY_MM_DD_HH_MM_SS));

        return sc;
    }
}
